package com.situ.mall.web.controller;


import com.situ.mall.common.entity.Member;
import com.situ.mall.common.service.IMemberService;
import com.situ.mall.common.util.SendSmsUtil;
import com.situ.mall.common.vo.TableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

//登录
@Controller
@RequestMapping("/")
public class LoginController {

    @Autowired
    private IMemberService memberService;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    //返回登录界面
    @GetMapping("/login")
    public String login() {
        return "login";
    }


    @PostMapping("/login")
    public String login(@RequestParam String username, String password,Model model){

        Object o = redisTemplate.opsForValue().get("err-"+username);
        if(o !=null){
            int i = (int) o;
            if(i==5){
                //给前台返回一个失败原因
                model.addAttribute("errMsg","两小时后重试");
                return "/login";
            }
        }
        boolean b = memberService.check(username, password);
        if (b){
            //一旦登陆成功把之前保存的失败次数删除
            redisTemplate.delete("err-"+username);
            return "redirect:/";
        } else {
            //在redis中记录 username 这个用户失败了
            //先判断之前有没有存在过
            if (o!=null){
                redisTemplate.opsForValue().set("err-"+username,(int) o + 1,2, TimeUnit.HOURS);
            }else {
                redisTemplate.opsForValue().set("err-"+username,1,2, TimeUnit.HOURS);
            }
            model.addAttribute("errMsg","还有两次机会");
            return "/login";
        }
    }

    //接受一个手机号给这个手机号发短信
    @PostMapping("/sendCode")
    @ResponseBody
    public void sendCode(String tel){
        //验证手机号有没有注册过
        Member m = memberService.selectByTel(tel);
        if (m!=null){//说明之前注册过
            //throw new BusinessException("手机号已被使用");
        }
        //使用发短信的工具类发短信
        int i = (int)(Math.random()*900000+100000);
        //redis存储
        redisTemplate.opsForValue().set("r-"+tel,i,5,TimeUnit.MINUTES);

        //参数  模板号，手机号，内容
        SendSmsUtil.sendMessage("",tel,String.valueOf(i));
    }

    @PostMapping("/register")
    public String register(String tel,String code){
        //先判断code对不对
         Object o = redisTemplate.opsForValue().get("r-"+tel);
         if(o==null || !((String)o).equals(code)){
             //throw new BusinessException("验证码不正确");
         }
         //派memberService去注册这个账号
        return "login";
    }

    @RequestMapping("/login/UpdatePassword")
    @ResponseBody
    public TableVO UpdatePassword(String oldPassword, String newPassword, String newPasswordAgain, HttpServletRequest request, HttpServletResponse response){
        return memberService.UpdatePassword(oldPassword,newPassword,newPasswordAgain,request,response);
    }

}
